package com.bocloud.cmp.driver.providers.os;

import java.util.List;
import java.util.UUID;

import org.openstack4j.api.Builders;
import org.openstack4j.model.common.ActionResponse;
import org.openstack4j.model.compute.Flavor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.bocloud.cmp.driver.providers.FlavorDriver;
import com.bocloud.cmp.model.FlavorModel;
import com.bocloud.common.model.BsmResult;
import com.google.common.collect.Lists;

public class OSFlavorDriver extends OSDriver implements FlavorDriver {

    private static Logger logger = LoggerFactory.getLogger(OSFlavorDriver.class);

    /**
     * 
     */
    public OSFlavorDriver() {
        super();
    }

    public OSFlavorDriver(String endpoint, String username, String password, String tenant, String domain,
            String project, Version version) {
        super(endpoint, username, password, tenant, domain, project, version);
    }

    @Override
    public BsmResult sync() {
        List<FlavorModel> models = Lists.newArrayList();
        List<? extends Flavor> flavors = Lists.newArrayList();
        if (!this.accessable()) {
            logger.error(this.getError());
            return new BsmResult(false, this.getError());
        }
        flavors = this.getClient().compute().flavors().list();
        if (null != flavors) {
            for (Flavor flavor : flavors) {
                FlavorModel model = new FlavorModel();
                model.setCpu(flavor.getVcpus());
                model.setMemory(flavor.getRam());
                model.setName(flavor.getName());
                model.setFamily(null);
                model.setDisk(flavor.getDisk());
                model.setFlavorId(flavor.getId());
                models.add(model);
            }
        }
        return new BsmResult(true, models, "success");
    }

    @Override
    public BsmResult create(FlavorModel flavor) {
        String id = UUID.randomUUID().toString();
        Flavor flavorResult = null;
        if (!this.accessable()) {
            logger.error(this.getError());
            return new BsmResult(false, this.getError());
        }
        try {
            flavorResult = this.getClient().compute().flavors().create(Builders.flavor().name(flavor.getName())
                    .ram(flavor.getMemory()).vcpus(flavor.getCpu()).disk(flavor.getDisk()).id(id).build());
            flavor.setFlavorId(flavorResult.getId());
            return new BsmResult(true, flavor, "创建成功!");
        } catch (Exception e) {
            logger.error("Flavor Exception {}", e);
            return new BsmResult(false, "创建失败!");
        }
    }

    @Override
    public BsmResult remove(String flavorId) {
        if (!this.accessable()) {
            logger.error(this.getError());
            return new BsmResult(false, this.getError());
        }
        ActionResponse response = this.getClient().compute().flavors().delete(flavorId);
        if (response.isSuccess()) {
            return new BsmResult(true, "配置删除成功！");
        } else {
            logger.error("remove flavor [{}] failed :{}", flavorId, response.getFault());
            return new BsmResult(false, "配置删除失败！");
        }
    }

}
